package com.hfzy.ihk.web.linkcall.biz;

import com.hfzy.ihk.common.constants.GlobalStatic;
import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchOperation;
import com.hfzy.ihk.common.core.elasticsearch.anotations.BestDoSupport;
import com.hfzy.ihk.common.core.elasticsearch.base.BestDoContextHolder;
import com.hfzy.ihk.common.core.elasticsearch.service.EsBaseServiceImpl;
import com.hfzy.ihk.common.web.WebResult;
import com.hfzy.ihk.facade.linkcall.entity.TestEntity;
import com.hfzy.ihk.facade.linkcall.entity.TestLog;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallOperations;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallRedisVersionKey;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallTables;
import com.hfzy.ihk.web.linkcall.dao.TestEntityRepository;
import org.elasticsearch.common.UUIDs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * Created by Administrator on 2018/7/19.
 *
 *  业务逻辑
 * @author wws
 */
@Component
public class TestEntityBiz extends EsBaseServiceImpl<TestEntity> {

    private static final Logger logger = LoggerFactory.getLogger(TestEntityBiz.class);

    @Autowired
    TestEntityRepository testEntityRepository;

    @Override
    protected ElasticsearchCrudRepository getReposity() {
        return testEntityRepository;
    }


    public String doTest(String str){
        return str+":";
    }

    @BestDoSupport
    public WebResult saveTestEntity(){

        WebResult result = new WebResult();
        /**/
        String txId = BestDoContextHolder.getTransactionId();

        TestEntity entity = new TestEntity();
        entity.setId(UUIDs.base64UUID());
        entity.setName("wws");

        if(txId != null){

            ElasticsearchOperation operation = new ElasticsearchOperation(txId,
                    LinkCallTables.TEST_LOG.getTableName(),
                    LinkCallOperations.addTestLog.getOperationName(),
                    new TestLog(UUIDs.base64UUID(),"wwsTestLog"));

            BestDoContextHolder.addOperation(operation);

        }

        boolean flag = createDataAndSetVersion(LinkCallRedisVersionKey.TABLE_TEST_Entity,
                LinkCallTables.TEST_ENTITY,
                entity.getId(),
                entity.getVersion(),
                ()-> testEntityRepository.save(entity));

        if(!flag){
            result.setCode(GlobalStatic.CODE_N);
            result.setMsg("保存出错");
            return result;
        }

        result.setCode(GlobalStatic.CODE_1);
        result.setMsg("成功");

        return result;
    }
}
